﻿#pragma once
#include "qtypes.h"


//----------------------------------
//QBrush class
//----------------------------------
namespace micro{
namespace gui{


class QBrush
{
	
	HBRUSH m_hBrush;

public:

	QBrush()
	{	
		m_hBrush	= nullptr;		
	}
	
	QBrush(QCOLOR crColor)
	{	
		m_hBrush = ::CreateSolidBrush(crColor);
		
	}


	~QBrush()	{	DeleteObject(); 	}

	void DeleteObject()
	{
		if ( m_hBrush ) 
		{
			::DeleteObject(m_hBrush);
			m_hBrush = nullptr;		
		}		
	}



	
	inline HBRUSH GetBrush() { return m_hBrush; }	
	inline operator HBRUSH() { return GetBrush();}

	
	void Attach(HBRUSH hBrush )
	{
		if(m_hBrush)
			::DeleteObject(m_hBrush);		
		m_hBrush = hBrush;		
		
	}
	
	HBRUSH Detach()
	{
		HBRUSH hBrush = m_hBrush;
		m_hBrush = nullptr;
		return hBrush;
	}

	
	HBRUSH CreateSolidBrush(QCOLOR crColor)
	{
		
		m_hBrush = ::CreateSolidBrush(crColor);		
		return m_hBrush;
	}

	HBRUSH CreateHatchBrush(int nIndex=HS_VERTICAL, QCOLOR crColor = 0 )
	{
		
		m_hBrush = ::CreateHatchBrush(nIndex, crColor);	
		return m_hBrush;
	}

	HBRUSH CreateBrushIndirect( LOGBRUSH* lpLogBrush)
	{
	
		m_hBrush = ::CreateBrushIndirect(lpLogBrush);		
		return m_hBrush;
	}

	HBRUSH CreatePatternBrush(HBITMAP hBitmap)
	{
		
		m_hBrush = ::CreatePatternBrush(hBitmap);		
		return m_hBrush;
	}

	HBRUSH CreateDIBPatternBrush( void* lpPackedDIB, UINT nUsage=DIB_PAL_COLORS)
	{
		if (lpPackedDIB == nullptr) return nullptr;
		m_hBrush=::CreateDIBPatternBrushPt(lpPackedDIB, nUsage);
				
		return m_hBrush;
	}
	
	HBRUSH CreateSysColorBrush(int nIndex)
	{
		
		m_hBrush = ::GetSysColorBrush(nIndex);		
		return m_hBrush;
	}

	

	bool GetLogBrush(LOGBRUSH* pLogBrush) 
	{
				
		return toBool( ::GetObject(m_hBrush, sizeof(LOGBRUSH), pLogBrush) );
	}	
};



 
}}//micro